package com.zut.controller.house;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.zut.entity.House;
import com.zut.service.HouseService;
import com.zut.service.impl.HouseServiceImpl;

/**
 * Servlet implementation class addHouseServlet
 */
@WebServlet("/addHouseServlet")
@MultipartConfig
public class addHouseServlet extends HttpServlet {
	HouseService hService = new HouseServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		House house = new House();
		house.setH_area(request.getParameter("h_area"));
		house.setH_estate(request.getParameter("h_estate"));
		house.setH_unitNumber(request.getParameter("h_unitNumber"));
		house.setH_floor(Integer.parseInt(request.getParameter("h_floor")));
		house.setH_roomNo(request.getParameter("h_roomNo"));
		house.setH_acreage(request.getParameter("h_acreage"));
		house.setH_direction(request.getParameter("h_direction"));
		house.setH_fitment(request.getParameter("h_fitment"));
		house.setH_isDoubleAir(Integer.parseInt(request.getParameter("h_isDoubleAir")));
		house.setH_limit(Integer.parseInt(request.getParameter("h_limit")));
		house.setH_facility(request.getParameter("h_facility"));
		house.setH_price(Double.parseDouble(request.getParameter("h_price")));
		System.out.println(request.getParameter("h_status"));
		house.setH_status(Integer.parseInt(request.getParameter("h_status")));
		
		//获取真实路径---存储路径
        String savePath = request.getServletContext().getRealPath("/image");
        //获取上传的文件集合
        List<Part> parts = (List<Part>) request.getParts();
        System.out.println("parts.size()=="+parts.size());
        
        String header = parts.get(13).getHeader("content-disposition");
        //获取文件名
        String fileName = getFileName(header);
        //把文件写到指定路径
        parts.get(13).write(savePath+File.separator+fileName);
        house.setH_img("/image/"+fileName);
   
		house.setH_address(request.getParameter("h_address"));
		
		hService.addHouse(house);
		response.sendRedirect(request.getContextPath()+"/findHouseServlet?currentPage=1");
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	 public String getFileName(String header) {
	        String[] tempArr1 = header.split(";");
	        String[] tempArr2 = tempArr1[2].split("=");
	        //获取文件名，兼容各种浏览器的写法
	        String fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\\")+1).replaceAll("\"", "");
	        return fileName;
	    }

}
